import {baseEffects, baseReducers, baseState} from "../../../../models/baseModel";
import AppService from "../../../../common/AppService";
import PageHelper from "utils/pageHelper";

export default {
  namespace: 'serverSet',

  state: {
    ...baseState,
  },

  reducers: {
    ...baseReducers
  },

  effects: {

    ...baseEffects,

    * getPageInfo({payload}, {call, put}) {
      const result = [];

      result.push({
        id: 1,
        name: "基础服务",
        ip: !!localStorage.getItem("SERVER") ? localStorage.getItem("SERVER") : AppService.SERVER,
        port: !!localStorage.getItem("SERVER_PORT") ? localStorage.getItem("SERVER_PORT") :AppService.SERVER_PORT,
      });
      result.push({
        id: 2,
        name: "设备服务",
        ip: !!localStorage.getItem("DEVICE_SERVER") ? localStorage.getItem("DEVICE_SERVER") :AppService.DEVICE_SERVER,
        port: !!localStorage.getItem("DEVICE_SERVER_PORT") ? localStorage.getItem("DEVICE_SERVER_PORT") :AppService.DEVICE_SERVER_PORT,
      });

      result.push({
        id: 3,
        name: "数据服务",
        ip: !!localStorage.getItem("DATA_SERVER") ? localStorage.getItem("DATA_SERVER") : AppService.DATA_SERVER,
        port: !!localStorage.getItem("DATA_SERVER_PORT") ? localStorage.getItem("DATA_SERVER_PORT") :AppService.DATA_SERVER_PORT,
      });
      yield put({
        type: 'list',
        payload: {
          data: PageHelper.wrapPage(result)
        }
      });
    },

    // 保存和修改
    * save({payload}, {put}) {
      console.log(payload);
      const {success, value} = payload;
      const {id, ip, port} = value;
      switch (id) {
        case 1:
          localStorage.setItem("SERVER", ip);
          localStorage.setItem("SERVER_PORT", port);
          break;
        case 2:
          console.log("修改设备服务", payload);
          localStorage.setItem("DEVICE_SERVER", ip);
          localStorage.setItem("DEVICE_SERVER_PORT", port);
          break;
        case 3:
          localStorage.setItem("DATA_SERVER", ip);
          localStorage.setItem("DATA_SERVER_PORT", port);
          break;
      }
      success();
      yield put({
        type: 'getPageInfo',
        payload
      });
    },


  }

}

